#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry
import json
#import pyLibs
# from unipath import Path

# import jarryLibs.dict2json as jd2j
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
# mport ScriptListWidget as slw
from TyhooToolCook.data.ui_element.ScriptListWidget import ScriptListWidget
# import icons_rc
from JTK.jcore import JPath, JJson
import TyhooToolCook
cook_data_path = TyhooToolCook.__COOK_PATH__.child('data')
# current_path = Path(__file__).parent


class ToolCookWidget(QWidget):
    instances = list()
    r'D:\SyncWorkspace\CGRepository\CGCodeRepos\JTKMayaPipline\scripts\TyhooToolCook\BarConfigCooks'
    r'D:\SyncWorkspace\CGRepository\CGCodeRepos\JTKMayaPipline\scripts\TyhooToolCook\data\BarConfigCooks'
    def __init__(self, parent=None):
        super(ToolCookWidget, self).__init__(parent)
        self.setMinimumSize(QSize(400, 1000))
        self.icon_path = cook_data_path.child('icons')
        self.bar_config_path = cook_data_path.child('BarConfigCooks')
        self.list_config_path = cook_data_path.child('ListConfigCooks')

        self.setObjectName('TyhooToolCookObject')
        self.setWindowTitle('TyhooToolCookW')
        self.central_widget = QWidget()

        self.main_layout = QVBoxLayout()
        self.title_label = QLabel()
        self.title_label.setPixmap(self.icon_path.child('title.jpg'))

        self.setup_tools()

        self.tab = QTabWidget()
        self.tab.setIconSize(QSize(20, 25))
        self.setup_tabs()

        self.logolayout = QHBoxLayout()
        self.logolab = QLabel('JTKCook.v1.0')
        self.logolab.setMaximumHeight(15)
        self.logolayout.addItem(QSpacerItem(5, 5, QSizePolicy.Expanding))
        self.logolayout.addWidget(self.logolab)

        self.main_layout.addWidget(self.title_label)
        self.main_layout.addWidget(self.tools_group)
        # self.main_layout.addWidget(self.tool_bar)

        self.main_layout.addWidget(self.tab)
        self.main_layout.addLayout(self.logolayout)

        parent.layout().addLayout(self.main_layout)
        # self.setLayout(self.main_layout)
        # self.central_widget.setLayout(self.main_layout)
        # self.setCentralWidget(self.central_widget)

    def setup_tools(self):
        # 配置常用工具包
        self.tools_group = QGroupBox()
        tools_layout = QHBoxLayout()
        self.tools_group.setLayout(tools_layout)
        tools_layout.setSpacing(0)
        tools_layout.setContentsMargins(0, 0, 0, 0)
        self.tools_group.setStyleSheet("QWidget{font-family:Microsoft YaHei;}")

        json_list = [j for j in self.bar_config_path.listdir('*.json')]
        for json in json_list:
            # 生成面板中快捷栏 bar
            bar_data = JJson(json).read()
            tool_bar = QToolBar(bar_data['name'])
            tool_bar.setContentsMargins(0, 0, 0, 0)
            cooks = bar_data['cooks']
            tool_bar.setIconSize(QSize(22, 22))

            none_line = QToolButton()
            none_line.setCheckable(True)
            none_line.clicked.connect(self.none_click)
            style_str = "QToolButton{image: url(:/icons/lineExpand.png);border: none;}"\
                                    "QToolButton::hover{background:rgb(82, 133, 166)}"\
                                    "QToolButton::checked{image: url(:/icons/lineCollection.png);}"
            none_line.setStyleSheet(style_str.replace(':/icons', self.icon_path.replace('\\','/')))

            none_line.setMaximumSize(QSize(8, 30))
            tool_bar.addWidget(none_line)

            for cook in cooks:
                if isinstance(cook, dict):
                    action = QAction(QIcon(self.icon_path.child(cook['icon'])), cook['label'], tool_bar)
                    action.command = cook['command']
                    tool_bar.addAction(action)
                    action.triggered.connect(self.toolbar_click)
                elif isinstance(cook, basestring):
                    self.tool_bar.addSeparator()
            tools_layout.addWidget(tool_bar)

        tools_layout.addItem(QSpacerItem(20, 30, QSizePolicy.Expanding, QSizePolicy.Minimum))

    def none_click(self):
        # 设置隐藏显示bar中的按钮
        line = self.sender()
        tool_bar = line.parent()
        for a in tool_bar.actions():
            if not isinstance(a, QWidgetAction):
                if line.isChecked():
                    a.setVisible(0)
                else:
                    a.setVisible(1)


    def setup_tabs(self):
        # 配置cook工具按钮
        json_list = [j for j in self.list_config_path.listdir('*.json')]
        for json in json_list:
            tab_data = JJson(json).read()
            # print tab_data
            tab_widget = QTabWidget()
            tab_widget.setIconSize(QSize(20, 25))
            tab_layout = QHBoxLayout()
            tab_list_widget = ScriptListWidget()
            tab_list_widget.setViewMode(QListView.IconMode)
            tab_list_widget.setStyleSheet("QWidget{font-family:Microsoft YaHei;}")

            # setup scirpt items
            cooks = tab_data['cooks']
            for cook in cooks:
                cook_item = QListWidgetItem()

                cook_item.setText(cook['name'])
                cook_item.setSizeHint(QSize(70, 60))
                icon = QIcon(self.icon_path.child(cook['icon']))
                cook_item.setTextAlignment(Qt.AlignCenter)
                cook_item.setIcon(icon)
                cook_item.command = cook['command']
                cook_item.setToolTip(cook['label'])

                # tab_list_widget.setupItemIcon(cook_item, 32, self.icon_path.child(cook['icon']))
                tab_list_widget.addItem(cook_item)
            tab_layout.addWidget(tab_list_widget)
            tab_widget.setLayout(tab_layout)
            icon = QIcon(self.icon_path.child(tab_data['icon']))
            self.tab.addTab(tab_widget, icon, tab_data['label'])

    def toolbar_click(self):
        action = self.sender()
        command = action.command
        exec (command)


class ToolCookWindow(QWidget):

    def __init__(self, parent=None):
        super(ToolCookWindow, self).__init__(parent)
        self.resize(QSize(0,500))
        self.main_layout = QHBoxLayout()
        self.setLayout(self.main_layout)
        self.main_widget = ToolCookWidget(self)
        # self.main_layout.addLayout(self.main_widget.main_layout)
        # self.setCentralWidget(self.main_widget)
        self.setStyleSheet("QWidget{font-family:Microsoft YaHei;}")

    def layout(self, *args, **kwargs):
        return self.main_layout


if __name__ == '__main__':
    app = QApplication([])
    my_ui = ToolCookWindow()
    my_ui.show()
    app.exec_()
